package com.ky.home.controller;


import cn.hutool.core.convert.NumberWithFormat;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.config.SystemConfig;
import com.ky.common.util.JwtUtil;
import com.ky.common.vo.R;
import com.ky.home.entity.Checkins;
import com.ky.home.entity.VO.CheckinVO;
import com.ky.home.mapper.CheckinsMapper;
import com.ky.home.service.CheckinsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author pl
 * @since 2024-06-18
 */
@RestController
@RequestMapping("/home/checkins")
public class CheckinsController {
    @Autowired
    private CheckinsMapper checkinsMapper;
    @Autowired
    CheckinsService checkinsService;


    @RequestMapping("/clickInRecord")
    public R clickInRecord(HttpServletRequest request) {
        String token = request.getHeader(SystemConfig.HEADER_TOKEN);
        Integer id = ((NumberWithFormat) JwtUtil.parseToken(token, "id")).intValue();
        QueryWrapper<Checkins> checkinsQueryWrapper = new QueryWrapper<Checkins>();
        checkinsQueryWrapper.eq("user_id", id);
        List<Checkins> checkins = checkinsMapper.selectList(checkinsQueryWrapper);
        if (checkins.size() > 0) {
            return R.ok(checkins);
        }
        return R.fail("查询失败");
    }

    @RequestMapping("/lastClickInRecord")
    public R lastClickInRecord() {
        int maxId = checkinsMapper.selectMaxId(); // 假设你已经实现了这个方法来获取最大ID
        if (maxId!=0) {
            CheckinVO checkinsByUserId = checkinsService.findCheckinsByUserId(maxId);
            // Checkins checkins = checkinsMapper.selectById(maxId);
            return R.ok(checkinsByUserId);
        } else {
            return R.fail("查询失败，没有找到记录");
        }
    }
    @RequestMapping("/todayClickIn")
    public R todayClickIn() {
        return R.ok(checkinsMapper.countTodayCheckin());
    }
}

